package com.larry.java;

import org.junit.Test;

/**
 * @ProjectName jvm_dc
 * @Author larry
 * @Date 9:50 2021/7/26
 * @Description i++和++i有什么区别
 **/
public class BytecodeInterview {
    @Test
    public void test1(){
        int i = 10;
        i++;
        ++i;
        System.out.println(i);
    }

    /**
     *  0 bipush 10
     *  2 istore_1
     *  3 iload_1
     *  4 iinc 1 by 1
     *  7 istore_1
     *  8 getstatic #2 <java/lang/System.out : Ljava/io/PrintStream;>
     * 11 iload_1
     * 12 invokevirtual #3 <java/io/PrintStream.println : (I)V>
     * 15 return
     */
    @Test
    public void test2(){
        int i = 10;
        i = i++;
        System.out.println(i);//10
    }

    /**
     *  0 iconst_2
     *  1 istore_1
     *  2 iload_1
     *  3 iload_1
     *  4 iinc 1 by 1
     *  7 imul
     *  8 istore_1
     *  9 getstatic #2 <java/lang/System.out : Ljava/io/PrintStream;>
     * 12 iload_1
     * 13 invokevirtual #3 <java/io/PrintStream.println : (I)V>
     * 16 return
     */
    @Test
    public void test3(){
        int i = 2;
        i *= i++;
        System.out.println(i);//4
    }


    @Test
    public void test4(){
        int k = 10;
        k = k + (k++) + (++k);
        System.out.println(k);//32
    }

    /**
     * 包装类对象的缓存问题
     * -128-127
     */
    @Test
    public void test5(){
        //Integer x = 5;
        //int y = 5;
        Integer i1 = 10;
        Integer i2 = 10;
        System.out.println(i1 == i2);//true

        Integer i3 = 128;
        Integer i4 = 128;
        System.out.println(i3 == i4);//false

        Boolean b1 = true;
        Boolean b2 = true;
        System.out.println(b1 == b2);//true

    }

    /**
     * String声明的字面量数据都放在字符串常量池中
     * jdk6中字符串常量池存放在方法区（即永久代中）
     * jdk7及以后字符串常量池存放在堆空间
     */
    @Test
    public void test6(){
        String str = new String("hello") + new String("world");
        String str1 = "helloworld";
        System.out.println(str == str1);//false
    }
}
